/*
 * @Author: Qimu
 * @Date: 2020-04-22 22:39:04
 * @LastEditors: Qimu
 * @LastEditTime: 2020-07-22 02:48:42
 * @Description: file content
 * @FilePath: \vue-admin-koa2\vue-admin\src\plugins\utils.js
 */
const utils = {
  //设置localStorage缓存
  local: {
    set(key, value) {
      localStorage.setItem(key, JSON.stringify(value));
    },
    get(key) {
      return JSON.parse(localStorage.getItem(key));
    },
    clear() {
      localStorage.clear();
    },
    remove(key) {
      localStorage.removeItem(key);
    }
  },
  //设置sessionStorage缓存
  session: {
    set(key, value) {
      sessionStorage.setItem(key, JSON.stringify(value));
    },
    get(key) {
      return JSON.parse(sessionStorage.getItem(key));
    },
    clear() {
      sessionStorage.clear();
    },
    remove(key) {
      sessionStorage.removeItem(key);
    }
  },
  /** 
  * 去除富文本标签方法
  * @param {String} str [富文本标签字符串] 
  */
  removeTag(str) {
    return str.replace(/<[^>]+>/g, '');
  },

  /** 
  * 格林尼治时间(GMT)转时间戳
  * @param {time} str [GMT字符串] 
  */
  GMTtoStr(time) {
    const date = new Date(time);
    const Year = date.getFullYear();
    const Month = date.getMonth() + 1 < 10 ? `0${(date.getMonth() + 1)}` : date.getMonth() + 1;
    const Day = date.getDate() < 10 ? `0${(date.getDate())}` : date.getDate();
    const Hours = date.getHours() < 10 ? `0${(date.getHours())}` : date.getHours();
    const Minutes = date.getMinutes() < 10 ? `0${(date.getMinutes())}` : date.getMinutes();
    const Seconds = date.getSeconds() < 10 ? `0${(date.getSeconds())}` : date.getSeconds();
    const dateLog = `${Year}-${Month}-${Day}`;
    const timeLog = `${Hours}:${Minutes}:${Seconds}`;
    return `${dateLog} ${timeLog}`
  },

  /** 
 * 电话号码验证方法
 * @param {String} str [电话号字符串] 
 */
  phoneCode(string) {
    const qqReg = /^(13|14|15|17|18|19)\d{9}$/;
    if (qqReg.test(string)) return true
  },

  /** 
 * 导出Excel方法
 * @param {Array} tHeader [导出excel表头] 
 * @param {Array} filterVal [导出excel字段数据] 
 * @param {Object} list [导出excel列表数据] 
 * @param {String} tableName [导出excel表名] 
 */
  export2Excel(tHeader, filterVal, list, tableName) {
    require.ensure([], () => {
      const { export_json_to_excel } = require('@/plugins/excel/Export2Excel');
      const data = list.map(v => filterVal.map(j => v[j]))
      console.log('data', data);
      export_json_to_excel(tHeader, data, tableName);
    })
  },

  //字母验证
  letter: {
    /** 
    * 小写字母验证方法
    * @param {String} str [小写字母字符串] 
    */
    lowerCase(str) {
      const reg = /^[a-z]+$/
      return reg.test(str)
    },

    /** 
    * 大写字母验证方法
    * @param {String} str [大写字母字符串] 
    */
    upperCase(str) {
      const reg = /^[A-Z]+$/
      return reg.test(str)
    },

    /** 
    * 大小写字母验证方法
    * @param {String} str [大小写字母字符串] 
    */
    alphabets(str) {
      const reg = /^[A-Za-z]+$/
      return reg.test(str)
    },

    //小写字母验证返回
    validateLowerCase() {
      return /^[a-z]+$/
    },
  },

  // 下划线转换驼峰
  toHump(name) {
    return name.replace(/_(\w)/g, function (all, letter) {
      return letter.toUpperCase();
    });
  },

  // 驼峰转换下划线
  toLine(name) {
    return name.replace(/([A-Z])/g, '_$1').toLowerCase();
  },
};

export default utils;